package com.example.java3s1.controller;

import com.example.java3s1.dao.MemoryDatabase;
import com.example.java3s1.entity.Person;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLOutput;
import java.util.List;

@RestController
public class Restful {
    @Resource
    private MemoryDatabase memoryDatabase;
   // @Cacheable(value = "see")
    @GetMapping("/persons")
    public List<Person> findAll(){
        System.out.println("开始查询数据库");
        return memoryDatabase.getPersons();
    }
    @GetMapping("/person/{name}")
    public Person getOne(@PathVariable String name){
       return memoryDatabase.getPersons()
                .stream()
                .filter(person -> person.getName().contains(name))
                .findFirst()
                .orElse(null);
    }
    @PutMapping("/person")
    public Person save(@RequestBody Person person){
        memoryDatabase.getPersons().add(person);
        return person;
    }
}
